package com.itfreer.data.general;

import com.itfreer.utils.StringUtils;

/**
 * 类功能说明 定义连接字符串解析类
 */
public class ConnectionInfo {

	public ConnectionInfo(String connectionStr) {
		GetPropertySet(connectionStr);
	}

	/**
	 * @Title: GetPropertySet
	 * @Description: 解析数据库连接的字符串获得IPropertySet
	 * @param @param strCon
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	private void GetPropertySet(String strCon) {
		if (!StringUtils.IsNullOrSpace(strCon)) {
			String[] arrcon = strCon.split(";");
			for (String item : arrcon) {
				if (StringUtils.IsNullOrEmpty(item))
					continue;

				String[] parameters = item.split("=");
				// 转换成大写
				String tempStr = parameters[0].toUpperCase().trim();
				if ("DATABASE".equals(tempStr)) {
					if (parameters.length > 1)
						database = parameters[1];
				} else if ("USERID".equals(tempStr)) {
					if (parameters.length > 1)
						userID = parameters[1];
				} else if ("PASSWORD".equals(tempStr)) {
					if (parameters.length > 1)
						password = parameters[1];
				} else if ("VERSION".equals(tempStr)) {
					if (parameters.length > 1)
						version = Integer.parseInt(parameters[1]);
				}
			}
		}
	}
	
	/**
	 * 工作空间类型
	 */
	private WorkspaceType workspaceType;

	public WorkspaceType getWorkspaceType() {
		return workspaceType;
	}

	public void setWorkspaceType(WorkspaceType value) {
		workspaceType = value;
	}

	/**
	 * 数据库名称或数据库文件路径
	 */
	private String database = "";

	public final String getDatabase() {
		return database;
	}

	public final void setDatabase(String value) {
		database = value;
	}

	private String userID = "";

	/**
	 * 用户名
	 */
	public final String getUserID() {
		return userID;
	}

	public final void setUserID(String userID) {
		this.userID = userID;
	}

	private String password = "";

	/**
	 * 密码
	 */
	public final String getPassword() {
		return password;
	}

	public final void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 版本
	 */
	private int version = 1;
	public final int getVersion() {
		return version;
	}
	public final void setVersion(int version) {
		this.version = version;
	}

	/**
	 * 获取连接字符串
	 */
	public String getConnectionStr() {
		StringBuilder sb = new StringBuilder();
		if (StringUtils.IsNullOrSpace(database))
			sb.append("DATABASE=;");
		else
			sb.append("DATABASE=" + database.toUpperCase().trim() + ";");

		if (StringUtils.IsNullOrSpace(userID))
			sb.append("USERID=;");
		else
			sb.append("USERID=" + userID + ";");

		if (StringUtils.IsNullOrSpace(password))
			sb.append("PASSWORD=;");
		else
			sb.append("PASSWORD=" + password + ";");

		sb.append("VERSION=" + version + ";");

		return sb.toString();
	}

	public Boolean Equals(Object obj) {
		ConnectionInfo info = (ConnectionInfo) obj;
		if (info == null)
			return false;

		if (workspaceType == info.getWorkspaceType()
				&& this.getConnectionStr() == info.getConnectionStr())
			return true;
		else
			return false;
	}

	public int GetHashCode() {
		String msg = "" + workspaceType.hashCode();
		msg += this.getConnectionStr();
		return msg.hashCode();
	}
}